package com.zhku.orders.service.impl;


import com.alipay.api.internal.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhku.base.exception.TaoTaoException;
import com.zhku.base.model.PageParams;
import com.zhku.base.model.PageResult;
import com.zhku.base.model.RestResponse;
import com.zhku.orders.feignclient.TtUserAddress;
import com.zhku.orders.feignclient.UserClient;
import com.zhku.orders.mapper.TtOrdersGoodsMapper;
import com.zhku.orders.mapper.TtOrdersLogisticsMapper;
import com.zhku.orders.mapper.TtOrdersMapper;
import com.zhku.orders.model.dto.*;
import com.zhku.orders.model.po.TtOrders;
import com.zhku.orders.model.po.TtOrdersGoods;
import com.zhku.orders.model.po.TtOrdersLogistics;
import com.zhku.orders.service.TtOrdersLogisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TtOrdersLogisticsServiceImpl extends ServiceImpl<TtOrdersLogisticsMapper, TtOrdersLogistics> implements TtOrdersLogisticsService {

    @Autowired
    TtOrdersLogisticsMapper ttOrdersLogisticsMapper;

    @Autowired
    TtOrdersMapper ttOrdersMapper;

    @Autowired
    TtOrdersGoodsMapper ttOrdersGoodsMapper;

    @Autowired
    UserClient userClient;

    @Override
    public ReturnLogisticsDto getUserLogistics(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new TaoTaoException("传入的物流表id为空");
        }
        LambdaQueryWrapper<TtOrdersLogistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TtOrdersLogistics::getStatus,"1").eq(TtOrdersLogistics::getId,id);
        TtOrdersLogistics ttOrdersLogistics = ttOrdersLogisticsMapper.selectOne(queryWrapper);
        if (ttOrdersLogistics == null) {
            throw new TaoTaoException("当前物流信息不存在");
        }
        ReturnLogisticsDto returnLogisticsDto = new ReturnLogisticsDto();
        BeanUtils.copyProperties(ttOrdersLogistics,returnLogisticsDto);
        RestResponse<TtUserAddress> address = userClient.getAddress(ttOrdersLogistics.getUserAddressId());
        TtUserAddress ttUserAddress = address.getResult();
        returnLogisticsDto.setName(ttUserAddress.getName());
        returnLogisticsDto.setPhone(ttUserAddress.getPhone());
        return returnLogisticsDto;
    }

    @Override
    public PageResult<ReturnLogisticsDto> queryUserLogisticsList(PageParams pageParams, QueryUserOrder queryUserOrder) {
        String userId = queryUserOrder.getUserId();
        if (StringUtils.isEmpty(userId)) {
            throw new TaoTaoException("传入的用户id为空");
        }
        LambdaQueryWrapper<TtOrdersLogistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TtOrdersLogistics::getUserId,userId).eq(TtOrdersLogistics::getStatus,"1");
        //创建page分页参数对象，参数：当前页码数，每页记录数
        Page<TtOrdersLogistics> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        Page<TtOrdersLogistics> ttOrdersPage = ttOrdersLogisticsMapper.selectPage(page, queryWrapper);
        List<TtOrdersLogistics> items = ttOrdersPage.getRecords();
        List<ReturnLogisticsDto> items2 = new ArrayList<>();
        for (TtOrdersLogistics item : items) {
            ReturnLogisticsDto returnLogisticsDto = new ReturnLogisticsDto();
            BeanUtils.copyProperties(item,returnLogisticsDto);
            RestResponse<TtUserAddress> address = userClient.getAddress(item.getUserAddressId());
            TtUserAddress ttUserAddress = address.getResult();
            returnLogisticsDto.setName(ttUserAddress.getName());
            returnLogisticsDto.setPhone(ttUserAddress.getPhone());
            items2.add(returnLogisticsDto);
        }
        long counts = ttOrdersPage.getTotal();
        PageResult<ReturnLogisticsDto> ttOrdersPageResult = new PageResult<>(items2,
                counts, pageParams.getPageNo(), pageParams.getPageSize());
        return ttOrdersPageResult;
    }

    @Override
    public PageResult<ReturnLogisticsDto> queryShopLogisticsList(PageParams pageParams, QueryShopOrder queryShopOrder) {
        String shopId = queryShopOrder.getShopId();
        if (StringUtils.isEmpty(shopId)) {
            throw new TaoTaoException("传入的店铺id为空");
        }
        LambdaQueryWrapper<TtOrdersLogistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TtOrdersLogistics::getShopId,shopId).eq(TtOrdersLogistics::getStatus,"1");
        //创建page分页参数对象，参数：当前页码数，每页记录数
        Page<TtOrdersLogistics> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        Page<TtOrdersLogistics> ttOrdersPage = ttOrdersLogisticsMapper.selectPage(page, queryWrapper);
        List<TtOrdersLogistics> items = ttOrdersPage.getRecords();
        List<ReturnLogisticsDto> items2 = new ArrayList<>();
        for (TtOrdersLogistics item : items) {
            ReturnLogisticsDto returnLogisticsDto = new ReturnLogisticsDto();
            BeanUtils.copyProperties(item,returnLogisticsDto);
            RestResponse<TtUserAddress> address = userClient.getAddress(item.getUserAddressId());
            TtUserAddress ttUserAddress = address.getResult();
            returnLogisticsDto.setName(ttUserAddress.getName());
            returnLogisticsDto.setPhone(ttUserAddress.getPhone());
            items2.add(returnLogisticsDto);
        }
        long counts = ttOrdersPage.getTotal();
        PageResult<ReturnLogisticsDto> ttOrdersPageResult = new PageResult<>(items2,
                counts, pageParams.getPageNo(), pageParams.getPageSize());
        return ttOrdersPageResult;
    }

    @Override
    public void shopConfirm(String id, AddressDto addressDto) {
        if (StringUtils.isEmpty(id)) {
            throw new TaoTaoException("传入的物流id为空");
        }
        TtOrdersLogistics ttOrdersLogistics = ttOrdersLogisticsMapper.selectById(id);
        if (ttOrdersLogistics == null) {
            throw new TaoTaoException("该物流表不存在");
        }
        String province = addressDto.getProvince();
        String city = addressDto.getCity();
        String county = addressDto.getCounty();
        String addressDetail = addressDto.getAddressDetail();
        ttOrdersLogistics.setStartaddress(province+city+county+addressDetail);
        ttOrdersLogistics.setShopStatus("1");
        ttOrdersLogistics.setShopFlag("1");
        int update = ttOrdersLogisticsMapper.updateById(ttOrdersLogistics);
        if (update <= 0) {
            throw new TaoTaoException("确认发货失败");
        }
    }

    @Override
    public void userConfirm(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new TaoTaoException("传入的物流id为空");
        }
        TtOrdersLogistics ttOrdersLogistics = ttOrdersLogisticsMapper.selectById(id);
        if (ttOrdersLogistics == null) {
            throw new TaoTaoException("该物流表不存在");
        }
        ttOrdersLogistics.setUserStatus("1");
        ttOrdersLogistics.setUserFlag("1");
        int update = ttOrdersLogisticsMapper.updateById(ttOrdersLogistics);
        if (update <= 0) {
            throw new TaoTaoException("确认收货失败");
        }
    }

    @Override
    public boolean verifyEvaluate(VerifyEvaluateDto verifyEvaluateDto) {
        String goodsId = verifyEvaluateDto.getGoodsId();
        if (StringUtils.isEmpty(goodsId)) {
            throw new TaoTaoException("远程调用校验用户是否有资格评价商品时传入的商品id为空");
        }
        String userId = verifyEvaluateDto.getUserId();
        if (StringUtils.isEmpty(userId)) {
            throw new TaoTaoException("远程调用校验用户是否有资格评价商品时传入的用户id为空");
        }
        LambdaQueryWrapper<TtOrdersLogistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TtOrdersLogistics::getUserId,userId).eq(TtOrdersLogistics::getStatus,"1").eq(TtOrdersLogistics::getUserStatus,"1").eq(TtOrdersLogistics::getUserFlag,"1").eq(TtOrdersLogistics::getGoodsId,goodsId);
        List<TtOrdersLogistics> list = ttOrdersLogisticsMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            return true;
        }
        return false;
    }
}
